package day01;

/**
 * @author feng
 * @Date 2021/10/13 15:21
 *   同步方法:
 *   1.非静态的方法:同步监视器是this，当前对象
 *   2.静态方法:当前类本身xxx.class
 *
 */
public class ThreadTest03 {


    private static int ticket = 100;
    private static Object  obj = new Object();   //类变量

    public void run() {

        while (true) {
            getTicket();
        }
    }
    private static synchronized void getTicket(){
            if (ticket > 0) {
                try {
                    Thread.sleep(100);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }

                System.out.println(Thread.currentThread().getName() + "票号为" + ticket);
                ticket--;
            }
        }
    }



class Test02 {
    public static void main(String[] args) {
        TicketTest t1 = new TicketTest();
        TicketTest t2 = new TicketTest();
        TicketTest t3 = new TicketTest();
        t1.setName("窗口1");
        t2.setName("窗口2");
        t3.setName("窗口3");
        t1.start();
        t2.start();
        t3.start();
    }


}
